package ali_oss;

import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.model.DownloadFileRequest;
import com.aliyun.oss.model.DownloadFileResult;
import com.aliyun.oss.model.ObjectMetadata;

public class TestMain {
    private static String getAccessKeyId_3rd = "LTAIURvKvbgVNfdj";
    private static String accessKeySecret_3rd = "w6yZLOfPzXix0LgtCAX90ulLdm8RQE";

    private static String endpoint = "oss-cn-shanghai.aliyuncs.com";
    private static String bucketName = "youku-media-test";

    public static void authDownload() {
        /**
         解释同上
         */
        OSS ossClient = new OSSClient(endpoint, getAccessKeyId_3rd, accessKeySecret_3rd);

        try {
            /**
             本次申请的bucketName为 "youku-media-test"
             key实际就是你下载的文件在服务器上存放的位置，例如上面说的xxx目录下的abc.ts
             那么这里key就是  "xxx/abc.ts"
             */
            DownloadFileRequest downloadFileRequest = new DownloadFileRequest(bucketName, "697444B07154471B9BB9C662A/03000600005C6E5571F5222011BA6ABFFC6D9A-83A4-4C1E-836D-AA5308D18358-00001.ts");
            // 下载到本地之后存放在本地的地址
            downloadFileRequest.setDownloadFile("d:/tmp/heibao-00001.ts");
            // Sets the concurrent task thread count 5. By default it's 1.
            downloadFileRequest.setTaskNum(5);
            // Sets the part size, by default it's 100K.
            downloadFileRequest.setPartSize(1024 * 1024 * 1);
            // Enable checkpoint. By default it's false.
            downloadFileRequest.setEnableCheckpoint(true);

            DownloadFileResult downloadResult = ossClient.downloadFile(downloadFileRequest);

            ObjectMetadata objectMetadata = downloadResult.getObjectMetadata();
            System.out.println(objectMetadata.getETag());
            System.out.println(objectMetadata.getLastModified());
            System.out.println(objectMetadata.getUserMetadata().get("meta"));

        } catch (OSSException oe) {
            System.out.println("Caught an OSSException, which means your request made it to OSS, "
                    + "but was rejected with an error response for some reason.");
            System.out.println("Error Message: " + oe.getErrorCode());
            System.out.println("Error Code:       " + oe.getErrorCode());
            System.out.println("Request ID:      " + oe.getRequestId());
            System.out.println("Host ID:           " + oe.getHostId());
        } catch (ClientException ce) {
            System.out.println("Caught an ClientException, which means the client encountered "
                    + "a serious internal problem while trying to communicate with OSS, "
                    + "such as not being able to access the network.");
            System.out.println("Error Message: " + ce.getMessage());
        } catch (Throwable e) {
            e.printStackTrace();
        } finally {
            ossClient.shutdown();
        }
    }

    public static void main(String[] args) {
        authDownload();
    }
}
